﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class admin_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] == null || Session["username"] == null)
        {
            Response.Redirect("login.aspx");
        }
        if (Request.QueryString["action"] != "" && Request.QueryString["action"] != null)
        {
            checkAction();
        }
        else
        {
            txt_er.Text = "";
        }
        
    }
    public void checkAction()
    {
        
        string action = Request.QueryString["action"];
        string id =Request.QueryString["id"];
        string[] permission = getPermission(Session["username"].ToString());
        foreach (string str in permission)
        {
            if (action == str)
            {
                txt_er.Text = "";
                if (action == "Comment_manager")
                {
                    Comment_manager1.Visible = true;
                }
                else if (action == "Article")
                {
                    Article1.Visible = true;
                }
                else if (action == "form_article")
                {
                    form_article1.Visible = true;
                }
                else if (action == "categories")
                {
                    list_categories.Visible = true;
                }
                else if (action == "article")
                {
                    Article1.Visible = true;
                }
                else if (action == "form_category")
                {
                    form_category1.Visible = true;
                    list_categories.Visible = false;
                }
                else if (action == "usergroup")
                {
                    usergroup1.Visible = true;
                }
                else if (action == "list_usergroup")
                {
                    list_usergroup1.Visible = true;
                }
                else if (action == "form_user")
                {
                    form_user1.Visible = true;
                }
                else if (action == "user")
                {
                    user1.Visible = true;
                }
                else if (action == "Topic")
                {
                    Topic1.Visible = true;
                }
                else if (action == "form_topic")
                {
                    form_topic1.Visible = true;
                }
                break;

            }
            else
            {
                txt_er.Text = "Bạn không có quyền truy cập";
            }
        }
        
    }
    protected void btnLogout_Click(object sender, ImageClickEventArgs e)
    {
        Session["username"] = null;
        Response.Redirect("login.aspx");
    }
    public string[] getPermission(string username)
    {
        UserBus userbus = new UserBus();
        User us = userbus.getUserByUserName(username);
        user_groupBus usergroupbus = new user_groupBus();
        user_group usergroup = usergroupbus.getUserGroupByID(Convert.ToInt32(us.User_group_id));
        return getArrayPermission(usergroup.Permission);
    }
    public string[] getArrayPermission(string txt)
    {
        string[] array = txt.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
        return array;
    }
    protected void btnUsers_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("default.aspx?action=user");
    }
}